package address

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"shop_webs/userop_web/api"
	"shop_webs/userop_web/forms"
	"shop_webs/userop_web/global"
	"shop_webs/userop_web/models"
	"shop_webs/userop_web/proto"
	"strconv"
)

func List(ctx *gin.Context) {
	userId := ctx.GetInt("userId")
	claims, _ := ctx.Get("claims")

	currentUser := claims.(models.MyClaims)
	zap.S().Infof("当前用户是%d,权限是%d", userId, currentUser.AuthorityId)
	var addressRequest proto.AddressRequest
	if currentUser.AuthorityId == 1 {
		addressRequest.UserId = int32(userId)
	}

	rsp, err := global.AddressClient.GetAddressList(context.Background(), &addressRequest)
	if err != nil {
		zap.S().Errorw("[List] 查询 【地址列表】失败", "msg", err.Error())
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	if rsp.Total == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"total": 0,
		})
		return
	}

	/*		前端格式
			{
				"total":12,
				"data":[
					{
						"id":1,
						"goods_id":421,
						"goods_name":421,
						"goods_price":421,
						"goods_image":421,
						"nums":421,
						"checked":421,
					}
				]
			}
	*/
	var addressList []map[string]interface{}
	for _, address := range rsp.Data {
		addressList = append(addressList, map[string]interface{}{
			"id":            address.Id,
			"user_id":       address.UserId,
			"province":      address.Province,
			"city":          address.City,
			"district":      address.District,
			"address":       address.Address,
			"signer_name":   address.SignerName,
			"signer_mobile": address.SignerMobile,
		})
	}

	ctx.JSON(http.StatusOK, gin.H{
		"total": rsp.Total,
		"data":  addressList,
	})
}
func New(ctx *gin.Context) {
	addressForm := forms.AddressForm{}
	if err := ctx.ShouldBind(&addressForm); err != nil {
		api.HandleValidatorError(err, ctx)
		return
	}

	rsp, err := global.AddressClient.CreateAddress(context.Background(), &proto.AddressRequest{
		UserId:       int32(ctx.GetInt("userId")),
		Province:     addressForm.Province,
		City:         addressForm.City,
		District:     addressForm.District,
		Address:      addressForm.Address,
		SignerName:   addressForm.SignerName,
		SignerMobile: addressForm.SignerMobile,
	})
	if err != nil {
		zap.S().Errorw("新建地址信息失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"id": rsp.Id,
	})
	return
}
func Update(ctx *gin.Context) {
	addressForm := forms.AddressForm{}
	if err := ctx.ShouldBind(&addressForm); err != nil {
		api.HandleValidatorError(err, ctx)
		return
	}

	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}
	_, err = global.AddressClient.UpdateAddress(context.Background(), &proto.AddressRequest{
		Id:           int32(i),
		Province:     addressForm.Province,
		City:         addressForm.City,
		District:     addressForm.District,
		Address:      addressForm.Address,
		SignerName:   addressForm.SignerName,
		SignerMobile: addressForm.SignerMobile,
	})
	if err != nil {
		zap.S().Errorw("更新地址失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{})
}
func Delete(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}
	_, err = global.AddressClient.DeleteAddress(context.Background(), &proto.AddressRequest{Id: int32(i)})
	if err != nil {
		zap.S().Errorw("删除地址失败")
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": "删除成功",
	})
}
